package com.qingbo.wu.oss.adapter.impl;

import com.qingbo.wu.oss.entity.FileInfo;
import com.qingbo.wu.oss.adapter.StorageAdapter;
import com.qingbo.wu.oss.utils.MinioUtil;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

/**
 * @Auther: qingbo.wu
 * @Date: 2024/6/1 17:24
 * @Description:
 */
//@Service(MinioStorageAdapter.BEAN)
public class MinioStorageAdapter implements StorageAdapter {

    public static final String BEAN = "minioStorageAdapter";

    @Resource
    private MinioUtil minioUtil;


    @Override
    @SneakyThrows
    public void createBucket(String bucket) {
        minioUtil.createBucket(bucket);
    }

    @Override
    @SneakyThrows
    public void uploadFile(MultipartFile uploadFile, String bucket, String objectName) {
        minioUtil.createBucket(bucket);
        if(StringUtils.isNoneBlank(objectName)){
            minioUtil.uploadFile(uploadFile.getInputStream(),bucket,objectName + "/" + uploadFile.getName());
        }else {
            minioUtil.uploadFile(uploadFile.getInputStream(),bucket,objectName);
        }
    }

    @Override
    @SneakyThrows
    public List<String> getAllBuckets() {
        return minioUtil.getAllBuckets();
    }

    @Override
    @SneakyThrows
    public List<FileInfo> getAllFile(String bucket) {
        return minioUtil.getAllFile(bucket);
    }

    @Override
    @SneakyThrows
    public InputStream downLoad(String bucket, String objectName) {
        return minioUtil.downLoad(bucket,objectName);
    }

    @Override
    @SneakyThrows
    public void deleteBucket(String bucket) {
        minioUtil.deleteBucket(bucket);
    }

    @Override
    @SneakyThrows
    public void deleteFile(String bucket, String objectName) {
        minioUtil.deleteFile(bucket,objectName);
    }
}
